<?php

class CategorieController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    	$layout=$this->_helper->layout();
    	$layout->setLayout('adminlayout');
    }

    public function indexAction()
    {
        $categories=new Application_Model_DbTable_Categories();
    	$this->view->categories =$categories->fetchAll();
    }

    public function addAction()
    {
    $form = new Application_Form_Categorie();
    $form->envoyer->setLabel('Ajouter');
    $this->view->form = $form;

    if ($this->getRequest()->isPost()) {
        $formData = $this->getRequest()->getPost();
        if ($form->isValid($formData)) {
            $nom_categorie = $form->getValue('nom_categorie');
            
            $categories = new Application_Model_DbTable_Categories();
            $categories->addCategorie($nom_categorie);

            $this->_helper->redirector('index');
        } else {
            $form->populate($formData);
        }
    }
    }

    public function updateAction()
    {
    $form = new Application_Form_Categorie();
    $form->envoyer->setLabel('Sauvegarder');
    $this->view->form = $form;

    if ($this->getRequest()->isPost()) {
        $formData = $this->getRequest()->getPost();
        if ($form->isValid($formData)) {
            $id = $form->getValue('id');
            $nom_categorie = $form->getValue('nom_categorie');
            $categories = new Application_Model_DbTable_Categories();
            $categories->updateCategorie($id, $nom_categorie);

            $this->_helper->redirector('index');
        } else {
            $form->populate($formData);
        }
    } else {
        $id = $this->_getParam('id', 0);
        if ($id > 0) {
            $categories = new Application_Model_DbTable_Categories();
            $form->populate($categories->getCategorie($id));
        }
    }
    }

    public function deleteAction()
    {
        // action body
    }


}







